package com.franklions.casdoor.example.context;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author flsh
 * @version 1.0
 * @date 2024/12/7
 * @since Jdk 1.8
 */
public class SecurityContextHolder {
    private static final ThreadLocal<Map<String, Object>> THREAD_LOCAL = new ThreadLocal<>();

    public static void set(String key, Object value)
    {
        Map<String, Object> map = getLocalMap();
        map.put(key, value);
    }

    public static String get(String key)
    {
        Map<String, Object> map = getLocalMap();
        return map.get(key).toString();
    }

    public static <T> T get(String key, Class<T> clazz)
    {
        Map<String, Object> map = getLocalMap();
        return (T)map.get(key);
    }

    public static Map<String, Object> getLocalMap()
    {
        Map<String, Object> map = THREAD_LOCAL.get();
        if (map == null)
        {
            map = new ConcurrentHashMap<String, Object>();
            THREAD_LOCAL.set(map);
        }
        return map;
    }
}
